package Volume1.ch6;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 实现cloneable接口的实体类
 * @Auther jacob
 * @Date 2020/8/8 02:13
 */
public class EmployeeCloneDemo implements Cloneable{

    private String name;
    private double salary;
    private Date hireDay;

    public EmployeeCloneDemo(String name, double salary) {
        this.name = name;
        this.salary = salary;
        this.hireDay = new Date();
    }

    public EmployeeCloneDemo clone() throws CloneNotSupportedException {
        // call object.clone()
        EmployeeCloneDemo cloned = (EmployeeCloneDemo) super.clone();

        // clone mutable fields
        cloned.hireDay = (Date) hireDay.clone();
        return cloned;
    }

    public void setHireDay(int year, int month, int day){
        Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
        hireDay.setTime(newHireDay.getTime());
    }

    public void raiseSalary(double byPercent){
        double raise = salary * byPercent / 100;
        salary += raise;
    }

    @Override
    public String toString() {
        return "EmployeeCloneDemo{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", hireDay=" + hireDay +
                '}';
    }
}
